<template>
  <div>
    {{ values.split_id }}
    <div>
      <template v-for="spl in split_id_options">
        <span :key="spl[0]">
          <a-divider type="vertical" />
          <a-button @click="handleMergeType(spl)">
            {{ spl[1] }}
          </a-button>
        </span>
      </template>
    </div>

    <a-divider type="vertical" />
    <a-button @click="create_sale_order"> 生成发票票面信息 </a-button>
  </div>
</template>

<script>
import api from '@/odoorpc'

export default {
  name: 'Sale2fp',
  components: {},
  mixins: [],
  props: {},

  data() {
    return {
      view: undefined,
      values: {},
      records_ods_line: [],
      records_line: [],
      split_id_options: []
    }
  },
  computed: {},

  watch: {},

  async created() {},

  async mounted() {},

  methods: {
    async _initMe_relation(view, fields_list) {
      const fields = view.fields
      const relations = {}
      for (const fld of fields_list) {
        const field_info = fields[fld]
        const relation = api.env.relation(field_info, {
          parent: view.view_info
        })

        await relation.load_views()

        relations[fld] = relation
      }
      view.load_relations_done(relations)
    },

    async _initMe_view(activeIds) {
      const view = api.env.wizardview('fp_sale.action_sale_to_fapiao', {
        active_ids: activeIds
      })
      // const fields =
      await view.load_fields()
      await this._initMe_relation(view, ['sale_order_ids'])

      return view
    },

    async _initMe_data(view) {
      const res = await view.onchange_new()
      console.log(res)
      const { values } = res

      return values
    },

    async _initMe_data_relation(view, values, fields_list) {
      const result = {}
      for (const fld of fields_list) {
        // console.log(fld)
        const relation_value = values[fld]
        // console.log(relation_value, view.relations)
        const relation_records = await view.relations[
          fld
        ].tree.read_for_new_x2m(relation_value)

        result[fld] = relation_records
      }

      return result
    },

    async initMe(activeIds) {
      const view = await this._initMe_view(activeIds)
      const values = await this._initMe_data(view)

      this.view = view

      this.values = values

      const realtion_data = await this._initMe_data_relation(view, values, [
        'sale_order_ids'
      ])

      console.log(values, realtion_data)

      const split_id_relation = api.env.relation(view.fields.split_id)

      const split_id_options = await split_id_relation.load_select_options({
        record: { ...values }
      })

      this.split_id_options = split_id_options
      console.log(split_id_options)
    },

    async handleMergeType(split_id) {
      console.log('split_id', split_id)
      // console.log(value, ...args)
      // console.log(this.invoice_type_value)
      const { values } = await this.view.onchange('split_id', {
        value: split_id
      })

      this.values = values
    },

    // 确认按钮
    async create_sale_order() {
      await this.view.wizard_button_clicked({
        type: 'object',
        name: 'create_fapiao'
      })

      this.$emit('change')
    }
  }
}
</script>

<style type="text/css" scoped></style>
